package com.zzedu.web.controller;

import com.alibaba.fastjson2.JSON;
import com.google.common.collect.Lists;
import com.zzedu.common.annotation.Log;
import com.zzedu.common.core.domain.entity.SysDictData;
import com.zzedu.common.core.service.ISysDictDataService;
import com.zzedu.common.enums.BusinessType;
import com.zzedu.common.enums.OperatorType;
import com.zzedu.common.utils.DictUtils;
import com.zzedu.common.utils.SecurityUtils;
import com.zzedu.device.req.GetUnitListByLevelPageList;
import com.zzedu.device.req.QueryPageZzPictureBook;
import com.zzedu.device.rsp.ApiZZCourseInfoVO;
import com.zzedu.device.rsp.ApiZzPictureBookVO;
import com.zzedu.domain.ZzSchool;
import com.zzedu.domain.ZzTeacher;
import com.zzedu.manager.ITeacherClassManager;
import com.zzedu.manager.IZzPictureBookManager;
import com.zzedu.result.Result;
import com.zzedu.result.ResultEnum;
import com.zzedu.service.IZzSchoolService;
import com.zzedu.service.IZzTeacherService;
import com.zzedu.utils.BaseReq;
import com.zzedu.utils.PageData;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 大屏#课程等级
 */
@RestController
@RequestMapping("course")
public class ApiCourseController {

    @Autowired
    ISysDictDataService dictDataService;

    @Autowired
    ITeacherClassManager teacherClassManager;

    @Autowired
    IZzPictureBookManager pictureBookManager;

    @Autowired
    IZzTeacherService zzTeacherService;

    @Autowired
    IZzSchoolService zzSchoolService;
    @Autowired
    DictUtils dictUtils;

    /**
     * 根据老师班级-返回课程等级列表
     *
     * @param req
     * @return
     */
    @PostMapping(value = {"/levelList"}, produces = {MediaType.APPLICATION_JSON_VALUE}, consumes = {MediaType.APPLICATION_JSON_VALUE})
    @Log(title = "根据老师班级-返回课程等级列表", businessType = BusinessType.APP_DEVICE_TEACHER, operatorType = OperatorType.MOBILE)
    public Result<List<SysDictData>> levelList(@Validated @RequestBody BaseReq req) {
        ZzTeacher zzTeacher = zzTeacherService.getById(SecurityUtils.getUserId());
        ZzSchool zzSchool = this.zzSchoolService.getById(zzTeacher.getSchoolId());
        List<String> levelList = JSON.parseArray(zzSchool.getLevel(), String.class);
        if (null == levelList || CollectionUtils.isEmpty(levelList)) {
            levelList = Lists.newArrayList();
        }
        Result<List<SysDictData>> result = new Result<>();
        List<SysDictData> sysDictDataList = dictUtils.getDictCache("level");
        List<String> finalLevelList = levelList;
        sysDictDataList = sysDictDataList.stream().filter(sysDictData -> finalLevelList.contains(sysDictData.getDictValue())).collect(Collectors.toList());

        result.setData(sysDictDataList);
        result.setResultCode(ResultEnum.SUCCESS.getCode());
        result.setResultMessage(ResultEnum.SUCCESS.getDesc());
        return result;
    }

    /**
     * 根据等级获取unit列表
     *
     * @return
     */
    @PostMapping(value = {"/getUnitListByLevel"}, produces = {MediaType.APPLICATION_JSON_VALUE}, consumes = {MediaType.APPLICATION_JSON_VALUE})
    @Log(title = "大屏-根据等级获取unit列表", businessType = BusinessType.APP_DEVICE_TEACHER, operatorType = OperatorType.MOBILE)
    public Result<PageData<ApiZZCourseInfoVO>> getUnitListByLevel(@Validated @RequestBody BaseReq<GetUnitListByLevelPageList> req) {
       return teacherClassManager.getUnitListByLevel(req.getData());
    }

    /**
     * 大屏-绘本列表
     *
     * @param req
     * @return
     */
    @PostMapping(value = {"/queryPage"}, produces = {MediaType.APPLICATION_JSON_VALUE}, consumes = {MediaType.APPLICATION_JSON_VALUE})
    @Log(title = "大屏-绘本列表", businessType = BusinessType.APP_DEVICE_TEACHER, operatorType = OperatorType.MOBILE)
    public Result<PageData<ApiZzPictureBookVO>> queryPageNews(@Validated @RequestBody BaseReq<QueryPageZzPictureBook> req) {
        return pictureBookManager.queryPageNews(req.getData());
    }

    /**
     * 小程序-绘本列表
     *
     * @param req
     * @return
     */
    @PostMapping(value = {"/queryPageToWxMini"}, produces = {MediaType.APPLICATION_JSON_VALUE}, consumes = {MediaType.APPLICATION_JSON_VALUE})
    @Log(title = "小程序-作业发送绘本列表", businessType = BusinessType.APP_DEVICE_TEACHER, operatorType = OperatorType.MOBILE)
    public Result<PageData<ApiZzPictureBookVO>> queryPageToWxMini(@Validated @RequestBody BaseReq<QueryPageZzPictureBook> req) {
        return pictureBookManager.queryPageToExMini(req.getData());
    }

}
